﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Data;

namespace website.PersonInfo
{
    /// <summary>
    /// PersonInfo 的摘要说明
    /// </summary>
    public class PersonInfo : IHttpHandler
    {
        string Action = "";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/plain";
            Action = RequstString("Action");

            if (Action == "initRoleInfo")
            {
                context.Response.Write(initRoleInfo());
            }

            else if (Action == "addPersonInfo")
            {
                context.Response.Write(addPersonInfo());
            }
            else if (Action == "GetPersonInfo")
            {
                string sidx = RequstString("sidx");
                string sord = RequstString("sord");
                string keyvalue = RequstString("keyword");
                string CustomerName = RequstString("CustomerName");
                context.Response.Write(getPersonInfo(sidx, sord, keyvalue, CustomerName));
            }
            else if (Action == "deletePersonInfo")
            {
                context.Response.Write(deletePersonInfo());
            }
            ///编辑用户初始化界面
            else if (Action == "initeditperson")
            {
                context.Response.Write(initeditperson());
            }
            // 保存编辑 用户
            else if (Action == "editPersonInfo")
            {
                context.Response.Write(editPersonInfo());
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public static string RequstString(string sParam)
        {
            return (HttpContext.Current.Request[sParam] == null ? string.Empty
                  : HttpContext.Current.Request[sParam].ToString().Trim());
        }

        public string initRoleInfo()
        {
            string customername = RequstString("CustomeName");
            string json = PersonBLL.initRoleInfo(customername);
            //string json = UserMenuBLL.GetMenuList();
            return json;
        }


        public string addPersonInfo()
        {
            string UserNo = RequstString("UserNo");
            string UserName = RequstString("UserName");
            string Password = RequstString("Password");
            string Tel = RequstString("Tel");
            string CustomerName = RequstString("CustomerName");
            string RoleName = RequstString("RoleName");

            string json = PersonBLL.addPersonInfo(UserNo, UserName, Password,Tel,CustomerName,RoleName);
           
            return json;
        }

        /// <summary>
        /// 保存编辑 用户
        /// </summary>
        /// <returns></returns>
        public string editPersonInfo()
        {
            string UserNo = RequstString("UserNo");
            string UserName = RequstString("UserName");
            string Password = RequstString("Password");
            string Tel = RequstString("Tel");
            string CustomerName = RequstString("CustomerName");
            string RoleName = RequstString("RoleName");

            string json = PersonBLL.editPersonInfo(UserNo, UserName, Password, Tel, CustomerName, RoleName);

            return json;
        }


        public string getPersonInfo(string sidx, string sord, string keyvalue, string CustomerName)
        {
            string strJson = "";
            DataTable dt = PersonBLL.getPersonInfo(sidx, sord, keyvalue, CustomerName);

            if (dt != null)
            {
                string page = RequstString("page");
                //String page =Re .getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数 
                string rows = RequstString("rows");  // 取得每页显示行数，,注意这是jqgrid自身的参数 
                int totalRecord = dt.Rows.Count; // 总记录数(应根据数据库取得，在此只是模拟) 
                int totalPage = totalRecord % Convert.ToInt16(rows) == 0 ? totalRecord
                / Convert.ToInt16(rows) : totalRecord / Convert.ToInt16(rows)
                + 1; // 计算总页数 
                int index = (Convert.ToInt16(page) - 1) * Convert.ToInt16(rows); // 开始记录数 
                int pageSize = Convert.ToInt16(rows);
                strJson = "{\"page\":" + page + ",\"total\": " + totalPage + "  ,\"records\":" + dt.Rows.Count.ToString() + ",\"rows\":[";
                for (int j = index; j < pageSize + index && j < totalRecord; j++)
                {
                    strJson += "{";
                    strJson += "\"id\":\"" + (j + 1).ToString() + "\",";
                    strJson += "\"cell\":";

                    strJson += "[";
                    strJson += "\"" + dt.Rows[j]["ID"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["UserNo"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["UserName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["Tel"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["RoleName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomerName"].ToString() + "\"";
                    strJson += "]";
                    strJson += "}";
                    if (j != pageSize + index - 1 && j != totalRecord - 1)
                    {
                        strJson += ",";
                    }
                }
            }
            else
            {
                strJson = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";

            }
            strJson = strJson.Trim().TrimEnd(new char[] { ',' });
            strJson += "]}";
            return strJson;
        }


        public string deletePersonInfo()
        {
            string UserID = RequstString("UserID");

            string json = PersonBLL.deletePersonInfo(UserID);

            return json;
        }

        public string initeditperson()
        {
            string PersonID = RequstString("PersonID");

            string json = PersonBLL.initeditperson(PersonID);

            return json;
        }


    }
}